import OpenGL.GL as gl
import numpy as np

from pygl.vertexbufferobject import VertexBufferObject
from pygl.elementbufferobject import ElementBufferObject

class VertexAttribute:
    def __init__(self, name: str, index: int, size: int, type: int, 
                 normalized: bool, offset: int) -> None:
        self.name = name
        self.index = index
        self.size = size
        self.type = type
        self.normalized = normalized
        self.offset = offset

class VertexArrayObject:
    def __init__(self) -> None:
        vao = np.empty(1, gl.GLuint)
        gl.glCreateVertexArrays(1, vao)
        self.vao_id = vao[0]

    def bind(self) -> None:
        gl.glBindVertexArray(self.vao_id)

    def delete(self) -> None:
        if gl.glIsVertexArray(self.vao_id):
            gl.glDeleteVertexArrays(1, self.vao_id)

    def setVertexBuffer(self, buffer: VertexBufferObject, bindingindex: int, 
                        offset: int, stride: int) -> None:
        gl.glVertexArrayVertexBuffer(
            self.vao_id, bindingindex, buffer.vbo_id, offset, stride)

    def setVertexAttribute(self, bindingindex: int, 
                           attrib: VertexAttribute) -> None:
        gl.glEnableVertexArrayAttrib(self.vao_id, attrib.index)
        gl.glVertexArrayAttribFormat(self.vao_id, attrib.index, 
            attrib.size, attrib.type, attrib.normalized, attrib.offset)
        gl.glVertexArrayAttribBinding(self.vao_id, attrib.index, bindingindex)

    def setElementBuffer(self, buffer: ElementBufferObject) -> None:
        gl.glVertexArrayElementBuffer(self.vao_id, buffer.ebo_id)

    def setBindingDivisor(self, bindingindex: int, divisor: int)-> None:
        gl.glVertexArrayBindingDivisor(self.vao_id, bindingindex, divisor)
  